/**
 * Created by liuqingxia on 2016/3/3.
 */

var id;

$(document).ready(function () {

    LTS.colFormatter.optFormat = function (v,row) {
        return '<a href="javascript:;" class="update-btn"><span class="label label-success" style="font-size: 14px"><i class="fa fa-edit"></i>编辑</span></a>&nbsp;' +
            '<a href="javascript:;" class="remove-btn"><span class="label label-primary" style="background-color: #DD6B55;font-size: 14px"><i class="fa fa-trash-o"></i>删除</span></a>&nbsp;';
    }

    LTS.colFormatter.userFormat = function (v,row) {
        var projectUsers = row['projectUser'].split(",");
        var result = projectUsers.join("; ");
        return result;
    }

    var findUrl = getRoot() + "/admin/project/find?time=" + new Date();
    var ltsTable = $("#project-table-container").ltsTable({
        url: findUrl,
        templateId: 'project-table'
    });

    $(document).on("click", "#searchBtn", function () {
        var params = {};

        $.each($('#project-find-form').find(".form-control"), function () {
            var name = $(this).attr("name");
            if(isNull(name)){
                return true;
            }
            var value = $(this).val();
            if(!isNull(value)){
                value = $.trim(value);
                params[name] = value;
            }
        });

        ltsTable.post(params, 1);
    });

    $(document).on("click",".remove-btn",function(){
        id = $(this).parent().parent().find(".id-tr").find(".id-span").html();
        $("#remove-tip-modal").modal("show");
    });

    $(document).on("click","#confirm-remove-btn",function(){
        var params = {};
        params['id'] = $.trim(id);
        var url = getRoot() + '/admin/project/remove';
        $.postHttp(params,url,function(json){
            if (json && json.success) {
                swal({
    				title : "操作成功",
    				text : json['message'],
    				type : "success"
    			}, function() {
    				$("#remove-tip-modal").modal("hide");
    	                var param = {};

    	                $.each($('#project-find-form').find(".form-control"), function () {
    	                    var name = $(this).attr("name");
    	                    if(isNull(name)){
    	                        return true;
    	                    }
    	                    var value = $(this).val();
    	                    if(!isNull(value)){
    	                        value = $.trim(value);
    	                        param[name] = value;
    	                    }
    	                });

    	                ltsTable.post(param, 1);
    			});
            } else {
                if (json) {
                    $("#remove-tip-modal").hide();
                    swal("失败",json['message'], 'error');
                }
            }
        });
        id = "";
    });

    $(document).on("click",".update-btn",function(){

        var projectId = $(this).parent().parent().find(".id-tr").find(".id-span").html();
        var findByIdUrl = getRoot() + "/project/findById";

        var params = {};
        params['id'] = projectId;

        $.postHttpSync(params,findByIdUrl,function(json){
            if (json && json.success) {
                var list = json['list'];
                params = list[0];
            } else {
                if (json) {
                    swal('失败',json['message'], 'error');
                    return;
                }
            }
        });

        $.each($('#editForm').parent().find(".form-control"), function () {
            var name = $(this).attr("name");
            if(isNull(name)){
                return true;
            }
            name = $.trim(name);
            $(this).val(params[name]);
        });

        var projectUsers = params['projectUser'].split(",");
        $(".projectUser").each(function(){
    		$(this).attr("checked",false);
    	});
        $.each($('.projectUser'), function () {
            var value = $(this).val();
            if(projectUsers.indexOf(value) != -1){
                this.checked=true;
            }
        });
        $("#project-edit-modal").modal("show");
    });

    $(document).on("click", "#edit-btn", function () {

        var params = {};
        params['projectName'] = $("#projectName").val();
        params['projectEmail'] = $("#projectEmail").val();
        params['projectPhone']= $("#projectPhone").val();
        params['id']= $("#id").val();
        var projectUsers = [];

        $.each($(".projectUser"), function () {
            if (this.checked) {
                projectUsers.push(this.value);
            }
        });
        if(isNull(projectUsers)){
        	swal("失败","请选择用户组成员", 'error');
        }
        params['projectUsers'] = projectUsers.join(",");
        var updateUrl = getRoot() + "/admin/project/update";
        $.postHttp(params,updateUrl,function(json){
            if (json && json.success) {
                swal({
    				title : "操作成功",
    				text : json['message'],
    				type : "success"
    			}, function() {
    				 $("#project-edit-modal").modal("hide");
    	                var param = {};

    	                $.each($('#project-find-form').find(".form-control"), function () {
    	                    var name = $(this).attr("name");
    	                    if(isNull(name)){
    	                        return true;
    	                    }
    	                    var value = $(this).val();
    	                    if(!isNull(value)){
    	                        value = $.trim(value);
    	                        param[name] = value;
    	                    }
    	                });

    	                ltsTable.post(param, pageNum);
    			});
            } else {
                if (json) {
                    swal("失败",json['message'], 'error');
                }
            }
        })

    });

    $("#searchBtn").trigger("click");
});
